<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="900px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="128px"
      :model="formData"
      :rules="rules"
      size="medium"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="机器类型" prop="machine">
            <el-select
              v-model="formData.machine"
              clearable
              filterable
              placeholder="机器类型"
            >
              <el-option
                v-for="(item, index) in machines"
                :key="index"
                :label="item.code"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机器名称" prop="name">
            <el-input
              v-model="formData.name"
              clearable
              placeholder="机器名称"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="源路径" prop="raw">
        <el-input v-model="formData.raw" clearable placeholder="源路径" />
      </el-form-item>
      <el-form-item label="拷贝路径" prop="copy">
        <el-input v-model="formData.copy" clearable placeholder="拷贝路径" />
      </el-form-item>
      <el-form-item label="目标路径" prop="target">
        <el-input v-model="formData.target" clearable placeholder="目标路径" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit('mainForm')">提交</el-button>
        <el-button @click="reset('mainForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
  import { updateOne as doEdit, addOne as doAdd } from '@/api/machine'
  import { machines } from '@/utils/constant'
  import { removeEmptyField } from '@/utils'
  export default {
    data() {
      return {
        formData: {
          id: '',
          machine: '',
          name: '',
          raw: '',
          copy: '',
          target: '',
        },
        rules: {
          machine: [
            { required: true, trigger: 'blur', message: '请选择机器类型' },
          ],
          name: [{ required: true, trigger: 'blur', message: '机器名称必须' }],
          raw: [{ required: true, trigger: 'blur', message: '源文件夹必须' }],
          copy: [
            { required: true, trigger: 'blur', message: '拷贝文件夹必须' },
          ],
          target: [
            { required: true, trigger: 'blur', message: '目标文件夹必须' },
          ],
        },
        activeName: 'first',
        machines,
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.formData = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.formData = {
          id: '',
          machine: '',
          name: '',
          raw: '',
          copy: '',
          target: '',
        }
        this.dialogFormVisible = false
      },
      submit() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.title === '编辑') {
              let { msg } = await doEdit(
                this.formData.id,
                removeEmptyField(this.formData)
              )
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            } else {
              let { msg } = await doAdd(removeEmptyField(this.formData))
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
